package com.xjrsoft.module.customer.erp_model.salesOrder.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.salesOrder.entity.ErpSalesorder;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.salesOrder.entity.ErpSalesorderentry;
import com.xjrsoft.module.customer.erp_model.salesOrder.entity.Inventoryinfo;
import com.xjrsoft.module.customer.erp_model.salesOrder.mapper.ErpSalesorderentryMapper;
import com.xjrsoft.module.customer.erp_model.salesOrder.dto.ErpSalesorderListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customer.erp_model.salesOrder.mapper.ErpSalesorderMapper;
import com.xjrsoft.module.customer.erp_model.salesOrder.service.IErpSalesorderService;
import com.xjrsoft.core.mp.base.BaseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.swing.text.html.parser.Entity;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-09-26
 */
@Service
@AllArgsConstructor
public class ErpSalesorderServiceImpl extends BaseService<ErpSalesorderMapper, ErpSalesorder> implements IErpSalesorderService {

	private ErpSalesorderentryMapper erpSalesorderentryMapper;

	@Override
	public IPage<ErpSalesorder> getPageList(ErpSalesorderListDto pageListDto) {
		Wrapper<ErpSalesorder> wrapper = Wrappers.<ErpSalesorder>query().lambda()				.eq(!StringUtil.isEmpty(pageListDto.getF_SalesCode()), ErpSalesorder::getFSalescode, pageListDto.getF_SalesCode())
				.ge(!StringUtil.isEmpty(pageListDto.getF_Orderdate_Start()), ErpSalesorder::getFOrderdate, pageListDto.getF_Orderdate_Start())
				.le(!StringUtil.isEmpty(pageListDto.getF_Orderdate_End()), ErpSalesorder::getFOrderdate, pageListDto.getF_Orderdate_End())
				.ge(!StringUtil.isEmpty(pageListDto.getF_DeliveryDate_Start()), ErpSalesorder::getFDeliverydate, pageListDto.getF_DeliveryDate_Start())
				.le(!StringUtil.isEmpty(pageListDto.getF_DeliveryDate_End()), ErpSalesorder::getFDeliverydate, pageListDto.getF_DeliveryDate_End())
				.eq(!StringUtil.isEmpty(pageListDto.getF_ClientId()), ErpSalesorder::getFClientid, pageListDto.getF_ClientId())
				.eq(!StringUtil.isEmpty(pageListDto.getF_SalesType()), ErpSalesorder::getFSalestype, pageListDto.getF_SalesType())
				.like(!StringUtil.isEmpty(pageListDto.getF_SalesPersonId()), ErpSalesorder::getFSalespersonid, pageListDto.getF_SalesPersonId())
				.eq(!StringUtil.isEmpty(pageListDto.getF_PaymentMethod()), ErpSalesorder::getFPaymentmethod, pageListDto.getF_PaymentMethod());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addErpSalesorder(ErpSalesorder erpSalesorder, List<ErpSalesorderentry> erpSalesorderentryList) {
		String erpSalesorderId = IdWorker.get32UUID();
		erpSalesorder.setFId(erpSalesorderId);
		erpSalesorder.setFStatus(1);
		erpSalesorderentryList.forEach(erpSalesorderentry -> erpSalesorderentry.setFSalesorderid(erpSalesorderId));
		this.saveBatch(erpSalesorderentryList, ErpSalesorderentry.class, ErpSalesorderentryMapper.class);
		boolean isSuccess = this.save(erpSalesorder);
		return isSuccess ? erpSalesorderId : null;
	}

	public  List<Inventoryinfo> getWarehouseInfo(String goodsId, String WarehouseId)
	{
		return erpSalesorderentryMapper.getWarehouseInfo(goodsId,WarehouseId);
	}

	@Override
	public boolean updateErpSalesorder(String id, ErpSalesorder erpSalesorder, List<ErpSalesorderentry> erpSalesorderentryList) {
		erpSalesorder.setFId(id);
		erpSalesorderentryList.forEach(erpSalesorderentry -> erpSalesorderentry.setFSalesorderid(id));
		erpSalesorderentryMapper.delete(Wrappers.<ErpSalesorderentry>query().lambda().eq(ErpSalesorderentry::getFSalesorderid, id));
		this.saveBatch(erpSalesorderentryList, ErpSalesorderentry.class, ErpSalesorderentryMapper.class);
		return this.updateById(erpSalesorder);
	}

	public List<ErpSalesorderentry> getErpSalesorderentryByParentId(String parentId){
		Wrapper wrapper = Wrappers.<ErpSalesorderentry>query().lambda().eq(ErpSalesorderentry::getFSalesorderid, parentId);
		return erpSalesorderentryMapper.selectList(wrapper);
	}
}
